<template>
	<view>
		<cu-custom :bgColor="'bg-' + theme.backgroundColor" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">直播间详情</block>
		</cu-custom>
		<scroll-view scroll-y class="scroll" refresher-enabled @refresherrefresh="refresherrefresh"
			@scrolltolower="scrolltolower" :style="customBarSty" scroll-anchoring :lower-threshold="80"
			:refresher-triggered="reftriggered">
			<view class="hander">
				<view class="title pb32">
					<view>
						<view class="name f30">
							<text>直播间名称：</text>
							<text>{{ liveInfo.liveName || "" }}</text>
						</view>
						<view class="code f28 text-gary1 mt16">
							<text>直播间编号：</text>
							<text>{{ liveInfo.liveNo }}</text>
						</view>
					</view>
					<view class="menu" @tap.stop="handExport">
						<view class="menu-btn">数据导出</view>
					</view>
				</view>
				<view class="num">
					<view class="num-item">
						<view class="f40">{{ liveInfo.liveUserNum }}</view>
						<view class="f28 mt10">观看人数</view>
					</view>
				</view>
				<view class="content">
					<view class="col">
						<view class="form-item">
							<text>答题人数：</text>
							<text class="ml6">{{ liveInfo.topicNum || 0 }}</text>
						</view>
						<view class="form-item">
							<text>答对人数：</text>
							<text class="ml6"> {{ liveInfo.topicYesNum || 0 }}</text>
						</view>
					</view>
					<view class="col">
						<view class="form-item">
							<text>红包支出：</text>
							<text class="ml6">{{ liveInfo.rewardPay || 0 }}</text>
						</view>
						<view class="form-item">
							<text>发放积分数：</text>
							<text class="ml6">{{ liveInfo.integralNum || 0 }}</text>
						</view>
					</view>
					<view class="col">
						<view class="form-item">
							<text>发放优惠券总数 :</text>
							<text class="ml6">{{ liveInfo.couponNum || 0 }}</text>
						</view>
						<view class="form-item">
							<text>发放奖品总数:</text>
							<text class="ml6">{{ liveInfo.lotteryNum || 0 }}</text>
						</view>
					</view>
				</view>
				<view class="times">
					<view class="col">
						<view class="form-item2">
							<text>直播开始时间：</text>
							<text class="ml6">{{ liveInfo.startTime }}</text>
						</view>
					</view>
					<view class="col">
						<view class="form-item2">
							<text>直播结束时间：</text>
							<text class="ml6">{{ liveInfo.endTime }}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="content p-all24">
				<view class="masters-list">
					<view class="item" v-for="(item, index) in tableList" :key="index">
						<view class="top">
							<view class="avatas">
								<image v-if="item.userHeadimgUrl" :src="item.userHeadimgUrl" mode="scaleToFill" />
								<image v-else src="@/static/public/img/user.png" mode="scaleToFill" />
							</view>
							<view class="info">
								<view> {{ item.nickName || "微信用户" }} </view>
								<view class="text-666">{{ item.userPhone || "暂无" }}</view>
							</view>
						</view>
						<view class="content">
							<view class="col">
								<view class="form-item">
									<text>答题数：</text>
									<text class="ml6">{{ item.topicNum || 0 }}</text>
								</view>
								<view class="form-item">
									<text>答对数：</text>
									<text class="ml6"> {{ item.topicYesNum || 0 }}</text>
								</view>
							</view>
							<view class="col">
								<view class="form-item">
									<text>领取红包：</text>
									<text class="ml6 text-price">{{ item.rewardPay || 0 }}</text>
								</view>
								<view class="form-item">
									<text>领取积分数：</text>
									<text class="ml6">{{ item.integralNum || 0 }}</text>
								</view>
							</view>
							<view class="col">
								<view class="form-item">
									<text>领取优惠券数：</text>
									<text class="ml6">{{ item.couponNum || 0 }}</text>
								</view>
								<view class="form-item">
									<text>获得奖品数：</text>
									<text class="ml6">{{ item.lotteryNum || 0 }}</text>
								</view>
							</view>
							<view class="col">
								<view class="form-item">
									<text>团长昵称：</text>
									<text class="ml6">{{ item.tzUserName || "" }}</text>
								</view>
								<view class="form-item">
									<text>团长电话：</text>
									<text class="ml6">{{ item.tzPhone || "" }}</text>
								</view>
							</view>
						</view>
					</view>
					<view :class="'cu-load ' + (loadmore ? 'loading' : 'over')"></view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import api from "../../../api/api.js";
	import util from "@/utils/util.js";
	import config from "@/config/env.js";
	const app = getApp();
	export default {
		data() {
			return {
				theme: app.globalData.theme,
				reftriggered: false,
				customBarSty: `height: calc(100vh - ${this.CustomBar}px);`,
				inputKey: "",
				loadmore: false,
				tzUserId: "",
				page: {
					current: 1,
					size: 5,
					total: 0,
				},
				tableList: [],
				liveInfo: {},
			};
		},
		onLoad() {
			let options = uni.getStorageSync("liveInfoData");
			this.tzUserId = options.tzUserId;
			this.liveInfo = options;
			this.page.current = 1;
			this.tableList = [];
			this.getPage();
		},
		methods: {
			handExport() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				});
				let that = this;
				uni.downloadFile({
					url: config.basePath + "/liveapi/excel/export/liveAndUserData?liveNo=" +
						this.liveInfo.liveNo,
					header: {
						"client-type": "MA", //客户端类型小程序
						"app-id": uni.getAccountInfoSync().miniProgram.appId, //小程序appId
						"third-session": uni.getStorageSync("third_session") ?
							uni.getStorageSync("third_session") : "",
					},
					success: (file) => {
						let fileManagerObj = uni.getFileSystemManager();
						let FileName = decodeURIComponent(file.header.FileName);
						const filePath = `${wx.env.USER_DATA_PATH}/${FileName}`;
						fileManagerObj.saveFile({
							tempFilePath: file.tempFilePath,
							filePath: filePath,
							success: (res) => {
								uni.hideLoading();
								uni.showModal({
									title: "提示",
									content: "ios:点击预览页面右上角三个点-->用其他应用打开-->存储到文件\n" +
										"安卓:点击预览页面右上角三个点-->保存到手机",
									showCancel: false,
									success: ({
										confirm,
										cancel
									}) => {
										if (confirm) {
											var filePath = res.savedFilePath;
											uni.openDocument({
												//新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
												filePath: filePath,
												showMenu: true,
												success: function(res) {
													console.log("打开文档成功");
												},
											});
										}
									},
									fail: (error) => {
										uni.hideLoading();
										uni.showToast({
											title: "导出失败",
											icon: "none",
										});
									},
								});
							},
						});
					},
					fail: (error) => {
						uni.hideLoading();
						uni.showToast({
							title: "导出失败",
							icon: "none",
						});
					},
				});
			},
			getPage(fn) {
				this.loadmore = true;
				let para = {
					...this.page,
					userPhone: this.inputKey,
					liveId: this.liveInfo.liveId,
				};
				api.pageUser(util.filterForm(para)).then((res) => {
					let tableList = res.data.records || [];
					this.tableList.push(...tableList);
					if (tableList.length < this.page.size) {
						this.loadmore = false;
					}
					typeof fn === "function" && fn(res);
				});
			},
			keyboardSearch() {
				this.tableList = [];
				this.page.current = 1;
				this.getPage();
			},
			scrolltolower() {
				if (this.loadmore) {
					this.page.current++;
					this.getPage();
				}
			},
			refresherrefresh(e) {
				this.reftriggered = true;
				this.tableList = [];
				this.page.current = 1;
				this.getPage(() => {
					this.reftriggered = false;
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.search {
		padding: 10rpx 0rpx 10rpx 32rpx;
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		border-radius: 16rpx;
		font-size: 24rpx;

		.icon {
			margin-right: 20rpx;
		}

		.search-ipt {
			flex: 1;

			input {
				border: none;
				outline: none;
				width: 100%;
				font-size: 28rpx;
				height: 70rpx;
				line-height: 70rpx;
			}
		}

		.search-btn {
			padding: 15rpx 32rpx;
			color: #fff;
			border-radius: 16rpx;
			margin-right: 10rpx;
			font-size: 24rpx;
		}
	}

	.masters-list {
		.item {
			margin: 0 0rpx 24rpx;
			background-color: #fff;
			border-radius: 16rpx;
			padding: 24rpx;

			.top {
				display: flex;
				align-items: center;

				.avatas {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;

					image {
						width: 100%;
						border-radius: 50%;
						height: 100%;
					}
				}

				.info {
					margin-left: 36rpx;
					height: 120rpx;
					padding: 16rpx 0;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
				}
			}
		}
	}

	.content {
		font-size: 24rpx;
		padding-bottom: 32rpx;
		border-bottom: 2rpx solid #f6f6f6;
	}

	.col {
		display: flex;
		align-items: center;
		margin-top: 16rpx;

		.form-item {
			width: 50%;
			color: #666;
		}

		.form-item1 {
			width: 100%;
			color: #666;
		}

		.form-item2 {
			width: 100%;
			color: #999;
		}
	}

	.num {
		display: flex;
		align-items: center;
		justify-content: flex-start;
		margin-top: 32rpx;
		padding-bottom: 24rpx;
		border-bottom: 2rpx solid #f6f6f6;

		.num-item {
			width: 50%;
			color: #666;
		}
	}

	.text-gary1 {
		color: #666;
	}

	.times {
		margin-top: 24rpx;
		margin-bottom: 12rpx;
		font-size: 24rpx;
		color: #999999;
	}

	.hander {
		background-color: #fff;
		border-radius: 0px 0px 24px 24px;
		padding: 32rpx;
	}

	.title {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 2rpx solid #f6f6f6;
	}

	.menu {
		display: flex;
		align-items: center;
		justify-content: flex-end;

		.menu-btn {
			font-size: 30rpx;
			padding: 20rpx;
			border-radius: 12rpx;
			margin-left: 20rpx;
			background: #ffffff;
			border-radius: 16rpx;
			border: 2rpx solid #fb2e1b;
			font-size: 28rpx;
			color: #fb2e1b;
		}
	}
</style>